#!/bin/bash

#env
JAVA_BIN=/usr/share/jdk1.8.0/bin
JAVAC=$JAVA_BIN/javac
JAVA=$JAVA_BIN/java
JAR=$JAVA_BIN/jar
KIOSK_TRUNK=http://dkbs-kiosk.googlecode.com/svn/trunk
UTIL_TRUNK=http://java-util-hspark.googlecode.com/svn/trunk
ROOT_PATH=`pwd`
WORKSPACE=$ROOT_PATH/workspace
BIN_PATH=$WORKSPACE/bin
JAR_FILE=$BIN_PATH/kiosk.jar
REV_SH=$BIN_PATH/rev.sh
EXEC="${JAVA} -jar ${JAR_FILE} &"

#check util version
echo Checking revision...
source $REV_SH
echo Jar util revision is $JAR_UTIL_REV
echo Jar kiosk revision is $JAR_KIOSK_REV
CURR_UTIL_VER=$((`svn info $UTIL_TRUNK | grep "^Revision:" | awk '{print $2}'`))
CURR_KIOSK_VER=$((`svn info $KIOSK_TRUNK | grep "^Revision:" | awk '{print $2}'`))
echo Current util version is $CURR_UTIL_VER
echo Current kiosk version is $CURR_KIOSK_VER

if [ $CURR_UTIL_VER -eq $JAR_UTIL_REV ]
then
  if [ $CURR_KIOSK_VER -eq $JAR_KIOSK_REV ]
  then
    echo All modifications applied. No build needed.
    $EXEC
    exit
  fi
fi

#remove exists sources
echo Remove exists sources...
rm -rf $WORKSPACE

#checkout sources
echo Exports source....
svn export $KIOSK_TRUNK $WORKSPACE/kiosk
svn export $UTIL_TRUNK $WORKSPACE/util

#source list
mkdir $WORKSPACE/bin
find $WORKSPACE/kiosk -name *.java > sources.list
find $WORKSPACE/util -name *.java >> sources.list

#compile
echo Compile....
$JAVAC -classpath /usr/share/jdk1.8.0/lib:/usr/share/jdk1.8.0/jre/lib -d ./workspace/bin @sources.list

#make jar
echo Make jar...
$JAR -cfvm $JAR_FILE ./workspace/kiosk/Manifest.mf -C ./workspace/bin/ . -C ./workspace/kiosk/resource/ .

#save revision value
echo JAR_UTIL_REV=$((CURR_UTIL_VER))$'\n'> $REV_SH
echo JAR_KIOSK_REV=$((CURR_KIOSK_VER))$'\n' >> $REV_SH 
chmod +x $REV_SH

#run
$JAVA -jar $JAR_FILE 

